package ua.maxicash.editors;

import ua.maxicash.service.WorkService;
import ua.maxicash.vo.JobType;

import java.beans.PropertyEditorSupport;

public class JobTypeEditor extends PropertyEditorSupport {

    //@Autowired
    WorkService workService;

    public JobTypeEditor(WorkService workService) {
        this.workService = workService;
    }

    @Override
    public String getAsText() {

        JobType jobType = (JobType) getValue();
        if (jobType != null) {
            return jobType.getIdJob().toString();
        } else {
            return "";
        }
    }

    @Override
    public void setAsText(String text) throws IllegalArgumentException {

        JobType jobType = workService.selectJobTypeById(Long.valueOf(text));
        if (jobType != null) {
            setValue(jobType);
        } else {
            throw new IllegalArgumentException();
        }

    }
}